﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Baopx_API;

namespace A
{
    public partial class M
    {
        public static DALDataContext d = new DALDataContext();

        public static void SetCookie(string name, string value)
        {
            HttpCookie cookie = new HttpCookie(name,value);
            cookie.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static string GetCookie(string name)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[name];
            if (cookie != null)
                return cookie.Value;
            return string.Empty;
        }

        public static void SaveAccountToCookie(Account account)
        {
            HttpCookie cookie = new HttpCookie(A.CookiesName.Account);
            cookie[A.CookiesName.UserId] = account.Id.ToString();
            cookie[A.CookiesName.UserRoleId] = account.UserRoleId.ToString();
            cookie[A.CookiesName.UserName] = account.UserName;
            cookie.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static Account GetAccountFromCookies()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[A.CookiesName.Account];
            if (cookie != null && cookie[A.CookiesName.UserId] != null)
            {
                Account account = new Account();
                account.Id = Convert.ToInt32(cookie[A.CookiesName.UserId]);
                account.UserName = cookie[A.CookiesName.UserName];
                account.UserRoleId = Convert.ToInt32(cookie[A.CookiesName.UserRoleId]);
                return account;
            }
            return null;
        }

        public static void DeleteAccountLoginInCookies()
        {
            HttpCookie cookie = new HttpCookie(A.CookiesName.Account);
            cookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        public static void RefreshCurrentPage()
        {
            HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString());
        }

        public static void Redirect(string url)
        {
            HttpContext.Current.Response.Redirect(url);
        }

        public static string GetUrlCurrent()
        {
            return HttpContext.Current.Request.Url.ToString();
        }

        public static string GetQueryString(string name)
        {
            return HttpContext.Current.Request.QueryString[name];
        }
    }
}
